package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.OSVo;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.service.OrderSettingService;
import cn.itcast.utils.DateUtils;
import cn.itcast.utils.POIUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService osService;

    /**
     *修改预约人数
     * @param orderSetting
     * @return
     */
    @PutMapping("updateOrderSettingNumber")
    public Result updateOrderSettingNumber(@RequestBody OrderSetting orderSetting){
        try {

            //调用更新方法
            int i= osService.update(orderSetting);
            if (i==0){
                //为0无影响行数 预约设置失败
         return new Result(false, MessageConstant.ORDERSETTING_FAIL);
            }
            //不然预约成功
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }

    /**
     * 预约设置中的上传文件方法
     * @param excelFile
     * @return
     */
    @PostMapping("upload")
    public Result upload(MultipartFile excelFile){
        if (excelFile==null){
            return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        }
        try {
            String fileName = excelFile.getOriginalFilename();  //获取文件名
            String fileXlsx = fileName.substring(fileName.length()-5);       //获取文件的后缀名为xlsx
            String fileXls = fileName.substring(fileName.length()-4);
            if(!(fileXlsx.equals(".xlsx") || fileXls.equals(".xls"))){   //如果不是excel文件
                return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
            }

            //传入参数

            //读取上传的excel文件，然后解析
            List<String[]> list = POIUtils.readExcel(excelFile);
            for (String[] strings : list) {//遍历进行赋值
                OrderSetting orderSetting = new OrderSetting();
                String date = strings[0].replace("/", "-");//替换符号
                orderSetting.setOrderDate(DateUtils.parseString2Date(date));//预约日期
                orderSetting.setNumber(Integer.parseInt(strings[1]));//预约时间
                int insert = osService.insert(orderSetting);//调用方法进行上传
                if (insert==0){
                    //影响行数为0 错误的查询
                    return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
                }

            }
            return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

    /**
     * 预约设置中的根据年月查询在这时间内的信息
     * @param date
     * @return
     */
    @GetMapping("getOrderSettingsByMonth")
    public Result getOrderSettingsByMonth(String date){
        try {
            //  根据时间 获得 数据
            List<OSVo> orderSettingVo = osService.findByMonth(date);
            if (orderSettingVo==null|| orderSettingVo.size()==0){
                //如果为空 或者 集合长度为0 获取数据失败
                return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL,orderSettingVo);
            }
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,orderSettingVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
    }



}

